#!/usr/bin/python3
#
# Copyright (c) 2020, 2022 NVI, Inc.
#
# This file is part of VLBI Field System
# (see http://github.com/nvi-inc/fs).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


import socket
#import netstring
import atexit
import os
import readline
import rlcompleter
import sys
import getopt
import string

historyPath = os.path.expanduser("~/pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

parms = {'-h': '127.0.0.1', '-p':5000, '-c': "dbe_alc=0:0:off:1:0:off;"}
try:
    opts, pargs = getopt.getopt(sys.argv[1:], "h:p:c:")
except getopt.GetoptError as msg:
    sys.exit(msg)

for o,v in opts:
    #print o,v
    parms[o] = v

host = str(parms['-h'])
port = int(parms['-p'])
mycmd = str(parms['-c'])
#print "Host:", host, "port:", port, "cmd:", mycmd

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect ((host,port))

arg = mycmd
while 1:
##     try:
##         arg = raw_input(">> ")
##     except EOFError:
##         print ''
##         break

    if arg in ('', ';'):
        continue

    #IF NOT QUIT
    if arg != "quit":

        # ADD ; TO END IF NOT THERE
        arg = arg.strip()
        if arg[-1] != ';':
            arg = arg + ';\n'
        else:
            arg = arg + '\n'

        # SEND COMMAND
        rv = s.send(arg.encode())
        if rv <= 0 :
            print("Fail to send ", rv)

        # WAIT FOR RESPONSE
        ret = s.recv(8192).decode()
        print(ret)
        arg = "quit"
    # ELSE TERMINATE SESSION
    else:
        break

s.close()
atexit.register(save_history)
